<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/8/25 10:53
 * Modified Time: 2019/8/25 10:53
 * Version: 2.0
 */

namespace App\Modules\MemberCard\Services;


use App\Lib\Utilities;
use App\Modules\MemberCard\Criterias\GetMemberCardByCardTypeCriteria;
use App\Modules\MemberCard\Repositores\BackupRepository;
use App\Modules\MemberCard\Repositores\CardTypeRepository;
use App\Modules\MemberCard\Repositores\MemberCardFlowRepository;
use App\Modules\MemberCard\Repositores\MemberCardRepository;
use App\Modules\Order\Repositories\RoyaltyRepository;
use App\Modules\Service\Repositories\CardTypeServiceRepository;
use App\Modules\System\Repositories\UserStoreRepository;
use Flower\Core\Repository\Eloquent\BaseService;

class MemberCardStatisticsService extends BaseService
{
    private $cardTypeRepository;
    private $userStoreRepository;
    private $cardTypeServiceRepository;
    private $memberCardRepository;
    private $backupRepository;
    private $royaltyRepository;
    private $memberCardFlowRepository;

    public function __construct(CardTypeRepository $cardTypeRepository,
                                UserStoreRepository $userStoreRepository,
                                CardTypeServiceRepository $cardTypeServiceRepository,
                                MemberCardFlowRepository $memberCardFlowRepository,
                                BackupRepository $backupRepository,
                                RoyaltyRepository $royaltyRepository,
                                MemberCardRepository $memberCardRepository)
    {
        $this->cardTypeRepository = $cardTypeRepository;
        $this->userStoreRepository = $userStoreRepository;
        $this->memberCardRepository = $memberCardRepository;
        $this->cardTypeServiceRepository = $cardTypeServiceRepository;
        $this->backupRepository = $backupRepository;
        $this->royaltyRepository = $royaltyRepository;
        $this->memberCardFlowRepository = $memberCardFlowRepository;
    }

    /*
     * 店铺会员卡统计
     */
    public function getMemberCardNum($store_id, $card_type_id = null)
    {
        $all_num = $this->memberCardRepository->getMemberCardNum($store_id, $card_type_id, 0);

        $exhausted_num = $this->memberCardRepository->getMemberCardNum($store_id, $card_type_id, 1);

        return [
            'all_num' => $all_num,
            'exhausted_num' => $exhausted_num
        ];
    }

    /*
     * 店铺老会员统计
     */
    public function getOldMemberCardNum($store_id)
    {
        $all_num = $this->memberCardRepository->getOldMemberCardNum($store_id, 0);

        $today_num = $this->memberCardRepository->getOldMemberCardNum($store_id, 1);

        return [
            'all_num' => $all_num,
            'today_num' => $today_num
        ];
    }

    /*
     * 店铺会员卡类状态统计
     */
    public function getStatusMemberCardNum($store_id)
    {
        $open_num = $this->cardTypeRepository->getStatusMemberCardNum($store_id, 1);
        $close_num = $this->cardTypeRepository->getStatusMemberCardNum($store_id, 0);

        return [
            'open_num' => $open_num,
            'close_num' => $close_num
        ];
    }

    /*
     * 某张会员卡充值总额及余额统计
     */
    public function getMemberCardAmount($member_card_id)
    {
        $member_card = $this->memberCardRepository->find($member_card_id);
        $attribute = $member_card->attribute;
        $balance = $member_card->balance;
        $unit = '元';
        $recharge_flow_sum = $this->memberCardFlowRepository->getRechargeFlowSum($member_card_id, $attribute);

        return [
            'recharge_flow_sum' => $recharge_flow_sum,
            'balance' => $balance,
            'unit' => $unit,
        ];
    }

    /*
     * 理发师会员卡统计
     */
    public function getBarberMemberCardNum($user_id, $store_id)
    {
        $store_num = $this->memberCardRepository->getMemberCardNum($store_id, null, 0);
        $user_num = $this->memberCardRepository->getMemberCardNum($store_id, null, 0, $user_id);
        return [
            'store_num' => $store_num,
            'user_num' => $user_num
        ];
    }

    /*
     * 理发师会员卡提成统计
     */
    public function getBarberMemberCardRoyalty($user_id, $store_id)
    {
        $user_num = $this->memberCardRepository->getMemberCardNum($store_id, null, 0, $user_id);
        $member_card_royalty = $this->royaltyRepository->getBarberMemberCardRoyalty($user_id, $store_id);
        return [
            'user_num' => $user_num,
            'member_card_royalty' => $member_card_royalty
        ];
    }

    /*
     * 获取某卡类办卡明细
     */
    public function getMemberCardByCardType($card_type_id, $pageSize)
    {
        $data = $this
            ->memberCardFlowRepository
            ->pushCriteria(new GetMemberCardByCardTypeCriteria($card_type_id))
            ->paginate($pageSize, ['*']);


        return Utilities::dealPage($data);
    }

    public function getBarberMemberCardApplyNum($store_id)
    {
        $all_num = $this->memberCardRepository->getMemberCardApplyNum($store_id);

        $today_num = $this->memberCardRepository->getMemberCardApplyNum($store_id,1);

        return [
            'all_num' => $all_num,
            'today_num' =>$today_num
        ];
    }
}